// 首页的reducer

// 初始值
const initialState = {
  // 用户频道数据
  userChannels: [],
  // 所有的频道数据
  allChannels: [],
  // 所有的频道的文章数据
  articles: {
    /*  1: {
      timestamp: '2021-10-2',
      list: [],
    },
    2: {
      timestamp: '2021-1-2',
      list: [],
    }, */
  },
}
export default function reducer(state = initialState, action) {
  const { type, payload } = action
  switch (type) {
    case 'SAVE_USER_CHANNELS':
      return {
        ...state,
        userChannels: payload,
      }
    case 'SAVE_ALL_CHANNELS':
      return {
        ...state,
        allChannels: payload,
      }
    case 'SAVE_ARTICLELIST':
      return {
        ...state,
        articles: {
          ...state.articles,
          // 每一个频道作为一个对象，里面保存该对象所有的文章信息
          [payload.channel_id]: {
            timestamp: payload.timestamp,
            list: payload.list,
          },
        },
      }
    case 'SAVE_MORE_ARTICLELIST':
      const channelId = payload.channel_id
      const oldList = state.articles[channelId].list
      return {
        ...state,
        articles: {
          ...state.articles,
          [payload.channel_id]: {
            timestamp: payload.timestamp,
            list: [...oldList, ...payload.list],
          },
        },
      }
    default:
      return state
  }
}
